package com.say.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.say.db.Person;
import com.say.db.PersonService;
import com.say.test.R;

/*
 	数据库操作练习
 */
public class DBOperateActivity extends Activity {
	private ListView listView;
	private PersonService personService;
	EditText editText1;
	EditText editText2;
	EditText editText3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dblistview);
		personService = new PersonService(this);
	
		listView = (ListView) this.findViewById(R.id.listView);
		editText1 = (EditText) this.findViewById(R.id.editText1);
		editText2 = (EditText) this.findViewById(R.id.editText2);
		editText3 = (EditText) this.findViewById(R.id.editText3);
		
		
		setContent1();
		makeLayout2();
	}
	
	private void makeLayout2() {
// ll:class android.widget.LinearLayout
// vg:class com.android.internal.policy.impl.PhoneWindow$DecorView

		LinearLayout ll = new LinearLayout(DBOperateActivity.this);
		System.out.println("ll:"+ ll.getClass());
		ll.setOrientation(LinearLayout.HORIZONTAL);
		/* 子物件居中排列 */
		ll.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
		ViewGroup vg;
		vg= (ViewGroup) ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);  
		vg.addView(ll);
		System.out.println("vg:"+ vg.getClass());
		
		Button but_total = new Button(this);
		but_total.setText("总数");
		but_total.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				long total = personService.getCount();
				Toast.makeText(DBOperateActivity.this, ""+total, Toast.LENGTH_SHORT).show();
			}
		});
		Button but_delete = new Button(this);
		but_delete.setText("删除全部");
		but_delete.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				personService.deleteAll();
			}
		});
		Button but_addBatch = new Button(this);
		but_addBatch.setText("批量添加");
		but_addBatch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				List<Person> list = new ArrayList<Person>(3);
				list.add(new Person("aaa", "13122223333", 250));
				list.add(new Person("bbb", "13255553333", 124));
				list.add(new Person("ccc", "13322223333", 133));
				for (Person person : list) {
					personService.save(person);
				}
			}
		});
		
		ll.addView(but_total);
		ll.addView(but_delete);
		ll.addView(but_addBatch);
	}

	private void setContent1() {
		final String C_NAME = "name";
		final String C_PHONE = "phone";
		final String C_AMOUNT = "amount";

		List<Person> beans = personService.getScrollData(0, 20);

		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (Person bean : beans) {
			HashMap<String, Object> item = new HashMap<String, Object>(2);
			item.put(C_NAME, bean.getName());
			item.put(C_PHONE, bean.getPhone());
			item.put(C_AMOUNT, bean.getAmount());
			data.add(item);
		}

		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.listview_item, new String[] { C_NAME, C_PHONE,
						C_AMOUNT }, new int[] { R.id.name, R.id.phone,
						R.id.amount });
		listView.setAdapter(adapter);
	}

	public void addPerson(View v) {
		String name = editText1.getText().toString();
		String phone = editText2.getText().toString();
		String as =  editText3.getText().toString();
		personService.save(new Person(name, phone, Integer.parseInt(as)));
		
		clearInput(null);
		reload(null);
	}

	public void clearInput(View v) {
		editText1.setText(null);
		editText2.setText(null);
		editText3.setText(null);
	}

	public void reload(View v) {
		setContent1();
	}

}
